package com.siqi.hr.system.common.chan;

import com.siqi.hr.system.common.sql.SField;
import com.siqi.hr.system.common.sql.STable;
import com.siqi.hr.system.common.utils.Common;
import org.apache.commons.lang.StringUtils;

/**
 * 通用查询列表数据查询sql语句拼接帮助类(xiaogq)
 */
public class TSqlListSelectStat extends TSQLSelectStat {
	
	public StringBuilder toSqlString() {
		return super.toSqlString();
	}

	public void init() {
		super.init();
		FieldChan temp = new FieldChan();
		STable mainTable = this.tableChan.getMianTable();
		
		for (SField field : this.fieldChan) {
			if (field.isQuote()) {//如果是引用字段
				if (field.getQuoteTable() != null && StringUtils.equals(field.getQuoteTable().getName(),"sys_custom_entity")) {
					STable table = new STable("sys_custom_entity",field.getName(), field.getTable().getByName());
					String fieldByName = null;
					if (!mainTable.getName().equals(field.getTable().getByName())) {
						fieldByName = field.getTable().getByName() + "___" + field.getName() + "_val";
					}else {
						fieldByName = field.getName() + "_val";
					}
					temp.add(new SField("inchinese", table, fieldByName));
					this.tableChan.add(table);
				} else if (field.getName().indexOf("sys_custom_entity_type") != -1) {
					STable table = new STable("sys_custom_entity_type",field.getName(), field.getTable().getByName());
					String fieldByName = null;
					if (!mainTable.getName().equals(field.getTable().getByName()))
						fieldByName = field.getTable().getByName() + "___" + field.getName() + "_val";
					else {
						fieldByName = field.getName() + "_val";
					}
					temp.add(new SField("inchinese", table, fieldByName));
					this.tableChan.add(table);
				}else if (field.getName().contains("sys_custom_selectbox")) {
					STable st = new STable("sys_custom_selectbox",field.getName(), field.getTable().getByName());
					String fieldByName = null;
					if (!mainTable.getName().equals(field.getTable().getByName())) {
						fieldByName = field.getTable().getByName() + "___" + field.getName() + "_val";
					}else {
						fieldByName = field.getName() + "_val";
					}
					temp.add(new SField("name", st, fieldByName));
					this.tableChan.add(st);
				} else if (field.isQuoteCustomTable()) {//如果是引用字段
					STable table = new STable(Common.getFkTableNameByField(field.getName()),field.getName(), field.getTable().getByName());
					String fieldByName = field.getName() + "_val";
					SField sf = new SField("name_varchar", table, fieldByName);
					temp.add(sf);
					this.tableChan.add(table);
				}
			} else if ((!field.getName().endsWith("_mutibox"))&& (SField.isSelectBox(field.getName()))) {//如果是下拉框字段
				STable st = new STable("sys_custom_selectbox", field.getName(),field.getTable().getByName());
				String fieldByName = null;
				if (!mainTable.getName().equals(field.getTable().getByName()))
					fieldByName = field.getTable().getByName() + "___" + field.getName() + "_val";
				else {
					fieldByName = field.getName() + "_val";
				}
				temp.add(new SField("name", st, fieldByName));
				this.tableChan.add(st);
			} else if (field.getName().endsWith("_date")) {//如果日期类型字段
				field.setFuction("date_format(@, '%Y-%m-%d')");
			} else if (field.getName().endsWith("_datetime") || field.getName().endsWith("_timestamp")) {//如果日期时间类型字段
				field.setFuction("date_format(@, '%Y-%m-%d %H:%i:%s')");
			} else if (StringUtils.endsWith(field.getName(), "_bit")) {
				if(field.getName().equals("flag_bit")){
					field.setFuction("if(@ is true, '停用', '启用') as " + field.getName());
				}else{
					field.setFuction("if(@ is true, '是', '否') as " +field.getName());
				}
			}
		}
		this.fieldChan.addAll(temp);
	}

	public TSqlListSelectStat() {
	}

	public TSqlListSelectStat(TSQLSelectStat tSelectStat) {
		setConditionChan(tSelectStat.getConditionChan());
		setFieldChan(tSelectStat.fieldChan);
		setTableChan(tSelectStat.getTableChan());
		setOrderby(tSelectStat.getOrderby());
		setLimit(tSelectStat.getLimit());
		setScope(tSelectStat.getScope());
		setTablecode(tSelectStat.getTablecode());
		setEmployee(tSelectStat.getEmployee());
		setDept(tSelectStat.getDept());
		setBootstrapQuery(tSelectStat.isBootstrapQuery());
		setSearchVal(tSelectStat.getSearchVal());
	}
}
